<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class adduser extends CI_Model {
	function __construct()
	{
		parent::__construct();
	}

	public function add_user()
	{
		$data=array(
			'USERNAME'=>$this->input->post('username'),
			'PASSWORD'=>md5($this->input->post('password')),
			'ROLE'=>$this->input->post('role'),
			'EMAIL'=>$this->input->post('email'),
			'STATUS'=>$this->input->post('status')
		  );
		$this->db->insert('user',$data);

		/*$sql = "INSERT INTO user (STATUS) VALUES('1')";
		$this->db->query($sql); */
	}
	 
	public function add_pegawai()
	{
		$datelawas = $this->input->post('tanggal_lahir');
		/* $tanggal=$date->format('Y-m-d'); */
		$tanggal = date("Y-m-d", strtotime($datelawas));
		
		$id_user = $this->selectWithParam('user', Array('USERNAME' => $this->input->post('username')));
		
		$data=array(
			'ID_USER' => $id_user[0]['ID_USER'],
			'NAMA_LENGKAP'=>$this->input->post('nama_lengkap'),
			
			'TEMPAT_LAHIR'=>$this->input->post('tempat_lahir'),
			'NIP'=>$this->input->post('NIP'),
			'TGL_LAHIR'=>$tanggal,
			'JENIS_KELAMIN'=>$this->input->post('jenis_kelamin'),
			'ALAMAT'=>$this->input->post('Alamat'),
			'TELP'=>$this->input->post('nomor_telepon'),
			'HP'=>$this->input->post('hp')
		  );
		$this->db->insert('pegawai',$data);
		
		$id_peg = $this->selectWithParam('pegawai', Array('NAMA_LENGKAP' => $this->input->post('nama_lengkap')));
		
		$data = array(
				   'ID_PEGAWAI' => $id_peg[0]['ID_PEGAWAI']
				);

		$this->db->where('ID_USER', $id_user[0]['ID_USER']);
		$this->db->update('user', $data);

		/* $sql = "INSERT INTO user (STATUS) VALUES('1')";
		$this->db->query($sql); */
	}

	public function selectAll($first, $last){
		$this->db->select('u.ID_USER, p.NAMA_LENGKAP, u.USERNAME, u.ROLE, u.STATUS');
		$this->db->from('user u');
		$this->db->join('pegawai p',"p.ID_USER=u.ID_USER","left");
		$this->db->offset($first);
		$query = $this->db->get(0, $last);
				
		return $query->result_array();
	}

	public function selectUserByID($idUser)
	{
		// $this->db->select('u.ID_USER, p.NAMA_LENGKAP, p.NIP, p.TEMPAT_LAHIR, p.TGL_LAHIR, p.JENIS_KELAMIN, p.ALAMAT, p.TELP, p.HP, u.EMAIL, u.USERNAME, u.ROLE, u.STATUS');
		// $this->db->from('user u, pegawai p');
		// $this->db->where('u.ID_USER = p.ID_USER');
		// $this->db->where("u.ID_USER = ".$idUser);
		// $query = $this->db->get();
		
		$query = $this->db->query('
		select
		user.ID_USER,
		user.USERNAME,
		user.EMAIL,
		user.ROLE,
		user.STATUS,
		pegawai.ID_USER,
		pegawai.NAMA_LENGKAP,
		pegawai.TEMPAT_LAHIR,
		pegawai.TGL_LAHIR,
		pegawai.NIP,
		pegawai.JENIS_KELAMIN,
		pegawai.TELP,
		pegawai.HP,
		pegawai.ALAMAT
		from user
		left join pegawai pegawai on user.ID_USER = pegawai.ID_USER
		where user.ID_USER = '.$idUser.'
		');
		
		return $query->result_array();
	}

	public function selectWithParam($table, $param)
	{
		$query = $this->db->get_where($table, $param);
		return $query->result_array();
	}
	 
	public function edit()
	{

		$datelawas = $this->input->post('tanggal_lahir');
		/* $tanggal=$date->format('Y-m-d'); */
		$tanggal = date("Y-m-d", strtotime($datelawas));
		
		$peg = array(
					'NAMA_LENGKAP'	=> $this->input->post('nama_lengkap'),
					'NIP'			=> $this->input->post('NIP'),
					'TEMPAT_LAHIR'	=> $this->input->post('tempat_lahir'),
					'TGL_LAHIR'		=> $tanggal,
					'JENIS_KELAMIN'	=> $this->input->post('jenisk'),
					'ALAMAT'		=> $this->input->post('alamat'),
					'TELP'			=> $this->input->post('nomor_telepon'),
					'HP'			=> $this->input->post('hp')
				);

		$this->db->where('ID_USER', $this->input->post('id_user'));
		$this->db->update('pegawai', $peg);
		
		if($this->input->post('password') == '*********'){
			$usr = array(
						'ROLE'		=> $this->input->post('role'),
						'STATUS'	=> $this->input->post('status')
					);
		}
		else{
			$usr = array(
						'ROLE'		=> $this->input->post('role'),
						'STATUS'	=> $this->input->post('status'),
						'PASSWORD'=>md5($this->input->post('password'))
					);
		}
		$this->db->where('ID_USER', $this->input->post('id_user'));
		$this->db->update('user', $usr);
	}
	
	public function getTotalRecords()
	{
		$this->db->select('u.ID_USER, p.NAMA_LENGKAP, u.USERNAME, u.ROLE, u.STATUS');
		$this->db->from('user u');
		$this->db->join('pegawai p',"p.ID_USER=u.ID_USER","left");
		$query = $this->db->get();
		
		return $query->num_rows();
	}
	

}

?>